package two.chapter_1.c1_3;

import edu.princeton.cs.algs4.StdOut;


public class Ex1_3_11 {

    public static void main(String [] args){
        String postfix="23*21-/341-*+";
        int result=evaluatePostfix(postfix);
        StdOut.println(result);
        assert 15==result;
    }

    private static int evaluatePostfix(String postfix){
        Stack<Integer> operand=new LinkStack<>();
        Stack<Integer> operator=new LinkStack<>();

        String inputs[]=postfix.split("");
        for (int i=0;i<inputs.length;i++){
            String input =inputs[i];
            char c=input.toCharArray()[0];
            if (Character.isDigit(c)){
                operand.push(Integer.parseInt(input));
            }else {
                int operand2=operand.pop();
                int operand1=operand.pop();
                int result=0;
                switch (input){
                    case "+":
                        result=operand1+operand2;
                        break;
                    case "-":
                        result=operand1-operand2;
                        break;
                    case "*":
                        result=operand1*operand2;
                        break;
                    case "/":
                        result=operand1/operand2;
                        break;
                }
                operand.push(result);
            }
        }
        return operand.pop();
    }
}
